<template>
  <div style="position: relative">
    <el-form :model="parameter.form" :rules="parameter.rules" ref="ruleForm" :label-width="parameter.labelWidth||'auto'">
      <div v-for="condItem in parameter.conditionList" :class="{'layerStyle': !condItem.layerStyle}" :style="condItem.layerStyle || {}">
        <el-form-item v-for="item in condItem.formList"
                      v-show="item.displayNone=== undefined || item.displayNone"
                      :key="item.key" :label="item.name" :prop="item.key"
                      :label-width="item.labelWidth || ''"
                      :class="{'elFormItemStyle': !item.elFormItemStyle}" :style="item.elFormItemStyle || {}">
          <el-input v-if="item.type === 'input'"
                    v-model="parameter.form[item.key]"
                    :disabled="item.disabled" :placeholder="item.placeholder||'请输入'"
                    :style="{width: item.width||'200px'}"
          />
          <el-input-number v-if="item.type === 'inputNum'"
                           v-model="parameter.form[item.key]" :min="item.min" :max="item.max"
                           :disabled="item.disabled" :controls-position='item.controlsPosition'
                           :controls='Boolean(item.controls)' @blur='()=>item.onBlur()'
                           :style="{width: item.width||'200px'}"
          />
          <el-select
            v-if="item.type === 'select'" :filterable="item.filterable"
            :loading="item.loading"
            v-model="parameter.form[item.key]"
            placeholder="请选择" :collapse-tags="item.collapseTags"
            :disabled="item.disabled"
            :multiple="item.multiple"
            :style="{width: item.width||'200px'}"
            @change="(v)=>item.onChange(v)"
            @focus="()=>item.focus()"
          >
            <el-option v-for="option in item.options" :label="option.label" :value="option.value"></el-option>
          </el-select>

          <el-date-picker
            v-if="item.type === 'date-picker'"
            v-model="parameter.form[item.key]"
            :value-format="item.format || 'yyyy-MM-dd HH:mm:ss'"
            :type="item.dateType || 'date'"
            placeholder=""
            :style="{width: item.width||'200px' }"
          ></el-date-picker>
          <el-date-picker
            v-if="item.type === 'datePickerRange'"
            v-model="parameter.form[item.key]"
            placeholder=""
            :value-format="item.format || 'yyyy-MM-dd HH:mm:ss'"
            :style="{width: item.width||'200px' }"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>

          <el-time-picker
            v-if="item.type === 'time-picker'"
            placeholder="选择时间"
            v-model="parameter.form[item.key]"
            :style="{width: item.width||'200px' }"
          ></el-time-picker>

          <el-switch v-if="item.type === 'switch'" v-model="parameter.form[item.key]"></el-switch>

          <el-checkbox-group v-if="item.type === 'checkbox-group'" v-model="parameter.form[item.key]">
            <el-checkbox v-for="option in item.options" :label="option" :key="option"></el-checkbox>
          </el-checkbox-group>

          <el-radio-group v-if="item.type === 'radio-group'" v-model="parameter.form[item.key]">
            <el-radio v-for="option in item.options" :label="option.value" :key="option.value">{{ option.label }}</el-radio>
          </el-radio-group>

          <el-input
            v-if="item.type === 'textarea'"
            type="textarea"
            v-model="parameter.form[item.key]"
            :style="{width: item.width||'100%'}"
          ></el-input>

          <el-cascader v-if="item.type === 'cascader'"
                       v-model="parameter.form[item.key]"
                       :style="{width: item.width||'200px'}"
                       :options="item.options"
                       @change="(v)=>item.onChange(v)"
                       @focus="()=>item.focus()"
          ></el-cascader>

          <el-upload v-if="item.type === 'upload'"
                     :action="item.action||'#'" :headers="item.headers"
                     :limit="item.limit" :file-list="item.fileList"
                     list-type="picture-card"
                     :on-change="(file, fileList)=>item.handleChange(file, fileList)"
                     :on-remove="(file, fileList)=>item.handleRemove(file, fileList)"
                     :on-success="(response,file, fileList)=>item.handleSuccess(response,file, fileList)"
                     :auto-upload="Boolean(item.action)">
            <i slot="default" class="el-icon-plus"></i>
          </el-upload>
          <slot v-if="item.type === 'slot'" :name="item.key" :slotitem="item"/>
        </el-form-item>
        <div v-if="condItem.btnList" :class="{'btnLayerStyle': !condItem.btnLayerStyle}" :style="condItem.btnLayerStyle || {}">
          <el-button v-for="item in condItem.btnList" :type="item.type" @click="item.action($refs.ruleForm)"
                     :loading="item.loading"  :style="item.style || {}" :icon="item.iconLeft">
            {{ item.name }}<i v-if="item.iconRight" :class="item.iconRight"/>
          </el-button>
        </div>
      </div>
    </el-form>
    <slot></slot>
  </div>
</template>
<script>
export default {
  props: {
    parameter: {
      type: Object,
      required: true,
      default: {
        conditionList: [
          {
            layerStyle: {   display: 'flex','flex-wrap': 'wrap',padding: '5px',gap: '20px 5px' },
            btnList: [ ],
            btnLayerStyle: {},
            labelWidth: '100px',
            formList:[ ],
          },
        ],
        form: {},
        rules: {},
      },
    },
  },

  methods:{
    validate(){
      return new Promise((resolve,reject) => {
        this.$refs.ruleForm.validate((valid)=>{
          //这里是一个promise的函数
          resolve(valid)
        })
      })
    }
  }
};
</script>
<style scoped lang="scss">
.layerStyle {
  display: flex;
  flex-wrap: wrap;
  padding: 5px;
  gap: 20px 5px;
}
.elFormItemStyle{
  margin-bottom: 0px!important;
  flex: 0 0 auto;
}
.btnLayerStyle{
  display: flex;
  flex-wrap: wrap;
  gap: 20px 5px;
}
</style>
